package com.example.service.impl;

import com.example.mapper.DeptMapper;
import com.example.mapper.EmpMapper;
import com.example.pojo.Dept;
import com.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;

@Service  //依赖注入——把当前这个类交给IOC容器管理，变成bean对象
public class DeptServiceimpl implements DeptService{
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    //@MyLog
    @Override
    public List<Dept> list(){
        return deptMapper.list();
    }
    @Transactional(rollbackFor = Exception.class)//spring事务管理
    @Override
    public void listDelete(Integer id) {
        deptMapper.listDelete(id);
//        if(true){
//            throw new RuntimeException("手动抛出异常");
//        }
        empMapper.deleteById(id);
    }

//    @Transactional(// 设置回滚的异常类型为Exception类
//    rollbackFor = Exception.class)//spring事务管理
    @Override
    public void insert(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public List<Dept> selectById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public  void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
         deptMapper.update(dept);
    }


}
